set more off
clear all


foreach year of numlist 1980 1990 2000 2011 2018 {

	*crosswalk file
	if `year'==2018 {
		use "Autor_Dorn_crosswalk/cw_puma2010_czone.dta", replace
		rename puma2010 puma_init
		gen statefip=floor(puma_init/100000)
		gen puma=puma_init-statefip*100000
	}
	if `year'==2011 | `year'==2000 {
		use "Autor_Dorn_crosswalk/cw_puma2000_czone.dta", replace
		rename puma2000 puma_init
		gen statefip=floor(puma_init/10000)
		gen puma=puma_init-statefip*10000
	}
	if `year'==1990 {
		use "Autor_Dorn_crosswalk/cw_puma1990_czone.dta", replace
		rename puma1990 puma_init
		gen statefip=floor(puma_init/10000)
		gen puma=puma_init-statefip*10000
	}
	if `year'==1980 {
		use "Autor_Dorn_crosswalk/cw_ctygrp1980_czone_corr.dta", replace
		rename ctygrp1980 puma_init
		gen statefip=floor(puma_init/1000)
		gen puma=puma_init-statefip*1000
	}
	drop puma_init
	save TEMP_crosswalk_`year', replace
	
	
	*ACS file
	use data_`year', replace
	if `year'==1980 {
		rename cntygp98 puma
	}
	drop if relate==13 /*drop inmates*/
	drop if empstatd==13 | empstatd==14 | empstatd==15 /*drop armed forces*/
	sort serial
	gen ind_kid=0
	replace ind_kid=1 if age<18 & relate>2
	by serial: egen N_kids=total(ind_kid) /*number of kids below 18*/
	*restrict ages and drop unnecessary variables
	keep if age>=25 & age<=64
	drop ind_kid countyfip raced educd related sample pernum
	cap drop multyear
	cap drop cbserial
	cap drop metarea*
	cap drop hrswork*
	cap drop cluster strata gq
	sort year serial
	replace ftotinc=. if ftotinc>=9999998
	by year serial: egen ftotinc_2=max(ftotinc)
	drop ftotinc
	rename ftotinc_2 ftotinc

	
	
	*merging
	joinby statefip puma using TEMP_crosswalk_`year', unmatched(both)
	if `year'==2011 {
		/*Katrina affected populaiton, so pumas can't be identified separately by regulation (each puma should have at least 100,000 people)*/
		replace czone=3300 if puma==77777 
		replace afactor=1 if puma==77777
		replace _merge=3 if puma==77777
	}
	
	keep if _merge==3
	drop _merge puma statefip
	replace hhwt=hhwt*afactor
	replace perwt=perwt*afactor
	drop afactor
	save TEMP_data_`year'_processed, replace
	erase TEMP_crosswalk_`year'.dta
}
